﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace ICSharpCode.WinFormsUI.Controls.Chart3D
{
    public partial class NChart3DControl : UserControl
    {
        int dragPointX = 0;
        int dragPointY = 0;
        bool dragging = false;
       
        double baseBeta;
        double baseTheta;

        Matrix3D matrix3D = null;        
        Pen pen = new Pen(Color.Black);
        List<Vector> chart_elements_list = new List<Vector>();

        public NChart3DControl()
        {
            InitializeChart();
            InitializeComponent();            
        }

        private void InitializeChart()
        {
            matrix3D = new Matrix3D(this);
            this.SetStyle(ControlStyles.ResizeRedraw |
              ControlStyles.OptimizedDoubleBuffer |
              ControlStyles.AllPaintingInWmPaint, true);
            this.UpdateStyles();  
        }

        protected override void OnPaint(PaintEventArgs e)
        {
            base.OnPaint(e);
            Graphics graphics = e.Graphics;
            DrawChartCoordinate(graphics);
            DrawChartElements(graphics);           
        }

        private void DrawChartCoordinate(Graphics g)
        {
            g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
            
            Vector vector1 = new Vector(-100, -100, 0, 100, -100, 0);
            Point2D p1 = matrix3D.Transform2D(vector1.point1);
            Point2D p2 = matrix3D.Transform2D(vector1.point2);            

            Vector vector2 = new Vector(100, -100, 0, 100, 100, 0);
            Point2D p3 = matrix3D.Transform2D(vector2.point1);
            Point2D p4 = matrix3D.Transform2D(vector2.point2);

            Vector vector3 = new Vector(100, 100, 0, -100, 100, 0);
            Point2D p5 = matrix3D.Transform2D(vector3.point1);
            Point2D p6 = matrix3D.Transform2D(vector3.point2);         

            Vector vector4 = new Vector(100, 100, 0, -100, 100, 0);
            Point2D p7 = matrix3D.Transform2D(vector4.point1);
            Point2D p8 = matrix3D.Transform2D(vector4.point2);

            Vector vector5 = new Vector(-100, 100, 0, -100, -100, 0);
            Point2D p9 = matrix3D.Transform2D(vector5.point1);
            Point2D p10 = matrix3D.Transform2D(vector5.point2);

            Vector vector6 = new Vector(-100, -100, 0, -100, -100, 50);
            Point2D p11 = matrix3D.Transform2D(vector6.point1);
            Point2D p12 = matrix3D.Transform2D(vector6.point2);

            Vector vector7 = new Vector(100, 100, 0, 100, 100, 50);
            Point2D p13 = matrix3D.Transform2D(vector7.point1);
            Point2D p14 = matrix3D.Transform2D(vector7.point2);

            Vector vector8 = new Vector(-100, 100, 0, -100, 100, 50);
            Point2D p15 = matrix3D.Transform2D(vector8.point1);
            Point2D p16 = matrix3D.Transform2D(vector8.point2);

            Vector vector9 = new Vector(100, 100, 50, -100, 100, 50);
            Point2D p17 = matrix3D.Transform2D(vector9.point1);
            Point2D p18 = matrix3D.Transform2D(vector9.point2);

            Vector vector10 = new Vector(-100, 100, 50, -100, -100, 50);
            Point2D p19 = matrix3D.Transform2D(vector10.point1);
            Point2D p20 = matrix3D.Transform2D(vector10.point2);

            g.DrawLine(pen, p1.X, p1.Y, p2.X, p2.Y);
            g.DrawLine(pen, p3.X, p3.Y, p4.X, p4.Y);
            g.DrawLine(pen, p5.X, p5.Y, p6.X, p6.Y);
            g.DrawLine(pen, p7.X, p7.Y, p8.X, p8.Y);
            g.DrawLine(pen, p9.X, p9.Y, p10.X, p10.Y);
            g.DrawLine(pen, p11.X, p11.Y, p12.X, p12.Y);
            g.DrawLine(pen, p13.X, p13.Y, p14.X, p14.Y);
            g.DrawLine(pen, p15.X, p15.Y, p16.X, p16.Y);
            g.DrawLine(pen, p17.X, p17.Y, p18.X, p18.Y);
            g.DrawLine(pen, p19.X, p19.Y, p20.X, p20.Y);

            //System.Drawing.Drawing2D.GraphicsPath face2_path = new System.Drawing.Drawing2D.GraphicsPath();
            //face2_path.AddLines(new PointF[]{            
            //    new PointF(p1.X, p1.Y), new PointF(p15.X, p15.Y), new PointF(p13.X, p13.Y),new PointF(p3.X, p3.Y), new PointF(p1.X, p1.Y)          
            //});
            //face2_path.CloseFigure();
            //g.FillPath(new SolidBrush(Color.FromArgb(200, SystemColors.Control)), face2_path);


            //System.Drawing.Drawing2D.GraphicsPath face1_path = new System.Drawing.Drawing2D.GraphicsPath();
            //face1_path.AddLines(new PointF[]{            
            //    new PointF(p1.X, p1.Y), new PointF(p12.X, p12.Y), new PointF(p16.X, p16.Y),new PointF(p15.X, p15.Y), new PointF(p1.X, p1.Y)          
            //});
            //face1_path.CloseFigure();
            //g.FillPath(new SolidBrush(Color.FromArgb(200, SystemColors.Control)), face1_path);

            //System.Drawing.Drawing2D.GraphicsPath face3_path = new System.Drawing.Drawing2D.GraphicsPath();
            //face3_path.AddLines(new PointF[]{            
            //    new PointF(p5.X, p5.Y), new PointF(p17.X, p17.Y), new PointF(p16.X-1, p16.Y+1),new PointF(p9.X-1, p9.Y-1), new PointF(p5.X+1, p5.Y-1)          
            //});
            //face3_path.CloseFigure();
            //g.FillPath(new SolidBrush(Color.FromArgb(200, SystemColors.Control)), face3_path);

            //var p = matrix3D.Transform2D(new Point3D(-100, 100, 0));
            //g.FillEllipse(Brushes.Red, p.X - 2, p.Y - 2, 4, 4);

        }

        private void DrawChartElements(Graphics g)
        {

        }

        protected override void OnMouseDown(MouseEventArgs e)
        {
            base.OnMouseDown(e);
            if (e.Button == MouseButtons.Left)
            {
                dragging = true;
                dragPointX = e.X;
                dragPointY = e.Y;
                baseTheta = matrix3D.Camera.Theta;
                baseBeta = matrix3D.Camera.Beta;
            }
        }

        protected override void OnMouseMove(MouseEventArgs e)
        {
            base.OnMouseMove(e);
            if (dragging)
            {                
                //matrix3D.Rotate(baseTheta - (e.Y - dragPointY) * 0.01,baseBeta + (e.X - dragPointX) * 0.01);
                //matrix3D.RotateY(baseTheta - (e.Y - dragPointY) * 0.01);
                matrix3D.RotateX(baseBeta + (e.X - dragPointX) * 0.01);
            }
        }

        protected override void OnMouseUp(MouseEventArgs e)
        {
            base.OnMouseUp(e);
            if (e.Button == MouseButtons.Left)
            {
                dragging = false;
            }
        }


    }
}
